package com.yl.chainStore.services.impl

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.yl.chainStore.entity.system.TableOtherColumn
import com.yl.chainStore.entity.system.TableOtherColumnGroup
import com.yl.chainStore.mapper.system.TableOtherColumnGroupMapper
import com.yl.chainStore.mapper.system.TableOtherColumnMapper
import com.yl.chainStore.pojo.vo.system.QueryHtmlConfigVo
import com.yl.chainStore.services.ICompanyDictionaryService
import com.yl.chainStore.services.ICompanyHtmlConfigService
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class CompanyHtmlConfigServiceImpl (
    private val tableOtherColumnGroupMapper: TableOtherColumnGroupMapper,
    private val tableOtherColumnMapper: TableOtherColumnMapper
        ): ICompanyHtmlConfigService {

    companion object {
        private val log = LoggerFactory.getLogger(ICompanyHtmlConfigService::class.java)
    }

    override fun createGroup(group:TableOtherColumnGroup): ResponseBody {
        log.info("页面配置 ==> 添加页面 ：group:{}",group)
        tableOtherColumnGroupMapper.insert(group)
        return returnSuccess()
    }


    override fun add(tableOtherColumn: TableOtherColumn): ResponseBody {
        log.info("页面配置 ==> 添加字段 ：tableOtherColumn:{}",tableOtherColumn)
        //todo 暂时不做其他判断处理
        tableOtherColumnMapper.insert(tableOtherColumn)
        return returnSuccess()
    }

    override fun del(id: Long) {
        log.info("页面配置 ==> 删除字段 ：id:{}",id)
        tableOtherColumnMapper.deleteById(id)
    }

    override fun getConfig(vo: QueryHtmlConfigVo): List<TableOtherColumn> {
        log.info("页面配置 ==> 获取页面配置 ：vo:{}",vo)
        val wrapper = QueryWrapper<TableOtherColumn>()
        wrapper.eq("company_id",vo.companyId)
        wrapper.eq("group_id",vo.groupId)
        wrapper.orderByAsc("order_num")
        return tableOtherColumnMapper.selectList(wrapper)
    }

    override fun getOneConfig(vo: QueryHtmlConfigVo): TableOtherColumn? {
        log.info("页面配置 ==> 获取页面配置 ：vo:{}",vo)
        val wrapper = QueryWrapper<TableOtherColumn>()
        wrapper.eq("company_id",vo.companyId)
        wrapper.eq("group_id",vo.groupId)
        if(vo.name != null && vo.name!!.isNotEmpty()){
            wrapper.eq("name",vo.name)
        }
        wrapper.orderByAsc("order_num")
        val list = tableOtherColumnMapper.selectList(wrapper)
        if(list.isEmpty()){
            return null
        }
        return list[0]
    }

    override fun bindAllToOtherCompany(companyId: Long, toCompanyId: Long): ResponseBody {
        val selectList =
            tableOtherColumnMapper.selectList(QueryWrapper<TableOtherColumn?>().eq("company_id", companyId))
        tableOtherColumnMapper.delete(QueryWrapper<TableOtherColumn?>().eq("company_id",toCompanyId))
        for(cl in selectList){
            cl.id = 0
            cl.companyId = toCompanyId
            tableOtherColumnMapper.insert(cl)
        }

        return returnSuccess()
    }
}
